﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class user_vercode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string vercode = RndNum(4);
        this.Session["vercode"] = vercode;
        ValidateCode(vercode);
    }
    private void ValidateCode(string vercode)
    {
        Bitmap oImg = new Bitmap(52, 24);
        Graphics oGra = Graphics.FromImage(oImg);
        MemoryStream ms = new MemoryStream();

        //背景颜色
        //oGra.Clear(Color.Green);
        //oGra.Clear(Color.FromName(@"#789e73"));
        oGra.Clear(Color.FromArgb(120, 158, 115));
        //文字字体
        //Font oFont=new Font("宋体",14,(FontStyle.Bold|FontStyle.Italic));
        Font oFont = new Font("宋体", 14, (FontStyle.Bold));

        oGra.DrawString(vercode, oFont, Brushes.White, 3, 2);
        oGra.DrawRectangle(Pens.Gray, 0, 0, oImg.Width - 1, oImg.Height - 1);

        oImg.Save(ms, ImageFormat.Gif);

        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());

        oGra.Dispose();
        oImg.Dispose();

        Response.End();
    }

    private string RndNum(int VcodeNum)
    {
        string VNum = "";
        //char[] Vchar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        char[] Vchar = new char[] { '0', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        Random ran = new Random();

        for (int i = 1; i < VcodeNum + 1; i++)
        {
            VNum += Vchar[ran.Next(30)];
        }

        return VNum;
    }
}
